//author:kakake
class tshisinterfaceDao {
    public data = {
        dbConn: null,
        WorkId:1,
        ChargeEmpID: 1322,//收费员ID
        ChargeEmpName: '机器人',//收费员名称
    }

    constructor(workid:number,dbconn:any,chargeEmpID:number,chargeEmpName:string){
        this.data.WorkId=workid;
        this.data.dbConn=dbconn;
        this.data.ChargeEmpID=chargeEmpID;
        this.data.ChargeEmpName=chargeEmpName;
    }

    //获取科室分类和科室
    getdept() {
        var strsql = "SELECT *  FROM BaseDeptLayer where workid=@workid ";
        var strsql2 = "SELECT *  FROM BaseDept a left join BaseDeptDetails b on a.DeptId=b.DeptID  where a.DelFlag = 0 and b.OutUsed=1 and a.workid=@workid ";

        var layer = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { workid: this.data.WorkId });
        var dept = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql2, { workid: this.data.WorkId });

        return { layer: layer, dept: dept };
    }

    //预约排班
    //显示医生以及医生各个时段的号源
    getdocOrderSched(_deptId: any, _empId?: any) {
        var deptId = _deptId || 0;
        var empId = _empId || 0;

        var strsql = "SELECT DeptID DeptId,DocEmpID DoctId,0 GoodNum,0 RegNum,\
                            dbo.fnGetDeptName(DeptID) DeptName,dbo.fnGetEmpName(DocEmpID) DocName FROM OP_DocSchedual\
                            where (DeptID=@deptId or 0>=@deptId) and (DocEmpID=@empId or 0>=@empId) AND\
                            SchedualDate>=CONVERT(varchar(10), GETDATE(), 120) AND\
                            SchedualDate<=CONVERT(varchar(10), DATEADD(DAY,6,GETDATE()),120)\
                            GROUP BY DeptID,DocEmpID";
        var dbparams = {
            deptId: deptId,
            empId: empId
        }
        var Items = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);


        Items.forEach(i => {
            strsql = "SELECT SchedualID RegId\
           ,DeptID DeptId,DocEmpID DoctId,dbo.fnGetDeptName(DeptID) DeptName,dbo.fnGetEmpName(DocEmpID) DocName \
           ,CONVERT(varchar(10), SchedualDate, 120) RegDate \
           ,Datepart(weekday, SchedualDate + @@DateFirst - 1) Week \
           ,0.01 RegFee \
           ,SchedualTimeRange TimeType \
           ,AppointNum AllCount \
           ,AppointNum-(SELECT COUNT(1) FROM OP_PatList where RegTimeRange=OP_DocSchedual.SchedualTimeRange and RegDeptID=OP_DocSchedual.DeptID and RegEmpID=OP_DocSchedual.DocEmpID and CONVERT(varchar(10), RegDate, 120)=CONVERT(varchar(10), OP_DocSchedual.SchedualDate, 120) and IsYy=1) HaveCount \
           ,1 scheduleStatus \
           FROM OP_DocSchedual where DeptID=@DeptId and DocEmpID=@DoctId AND \
           SchedualDate>=CONVERT(varchar(10), GETDATE(), 120) AND SchedualDate<=CONVERT(varchar(10), DATEADD(DAY,6,GETDATE()),120)";
            var dbparams2 = {
                DeptId: i.DeptId,
                DoctId: i.DoctId
            }
            i.scheduleStatus = 0;
            i.ItemA = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams2);
        });

        return Items;
    }

    //排班时间段
    scheduleTimeRange(_regid) {

        //1.查询指定号源的时段
        var strsql = "SELECT TOP 1 \
             SchedualTimeRange Timeid \
             ,case SchedualTimeRange WHEN 1 then '8:30' when 2 then '14:00'  else '18:00' end StartTime \
             ,case SchedualTimeRange WHEN 1 then '12:00' when 2 then '18:00' else '23:00' end EndTime \
             ,AppointNum AllCount \
             ,AppointNum-(SELECT COUNT(1) FROM OP_PatList where RegTimeRange=OP_DocSchedual.SchedualTimeRange and RegDeptID=DeptID and RegEmpID=DocEmpID and CONVERT(varchar(10), RegDate, 120)=CONVERT(varchar(10), SchedualDate, 120) and IsYy=1) HaveCount \
             FROM OP_DocSchedual where SchedualID=@regid";
        var dbparams = {
            regid: _regid
        }
        var Items = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        return Items;
    }

     //得到当前结账ID,iAccountType:0挂号收费 1会员充值
    add_regCost_GetAccountID() {

        var AccountID = 0;
        var LastDate;

        var OP_Account = {};//门诊交款对象
        var CurrDate = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");//当前日期


        let strsql = "SELECT AccountID,LastDate FROM OP_Account WHERE AccountEmpID=@OperatorID AND AccountType=0 and AccountFlag=0 and WorkID=@WorkId";
        let dbparams = {
            OperatorID: this.data.ChargeEmpID,
            WorkId: this.data.WorkId
        }

        let result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        if (result) {
            AccountID = result["AccountID"];
        }
        else {
            //不存在未交款ID，一般都是第一笔
            strsql = "SELECT TOP 1 AccountID,LastDate FROM OP_Account WHERE AccountEmpID=@OperatorID AND AccountType=0 and AccountFlag=1 and WorkID=@WorkId \
                        ORDER BY AccountID DESC";

            let result2 = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
            if (result2) {
                LastDate = result["LastDate"];
            } else {//不存在上次缴费时间，这是这个收费员的第一笔
                LastDate = CurrDate;
            }

            OP_Account['AccountID'] = 0;
            OP_Account['LastDate'] = LastDate;
            OP_Account['TotalFee'] = 0;
            OP_Account['CashFee'] = 0;
            OP_Account['PromFee'] = 0;
            OP_Account['PosFee'] = 0;
            OP_Account['AccountEmpID'] = this.data.ChargeEmpID;
            OP_Account['AccountDate'] = CurrDate;
            OP_Account['AccountFlag'] = 0;
            OP_Account['ReceivFlag'] = 0;
            OP_Account['ReceivEmpID'] = 0;
            OP_Account['ReceivDate'] = "1900-01-01";
            OP_Account['ReceivBillNO'] = "";
            OP_Account['WorkID'] = this.data.WorkId;
            OP_Account['InvoiceCount'] = 0;
            OP_Account['RefundInvoiceCount'] = 0;
            OP_Account['RoundingFee'] = 0;
            OP_Account['AccountType'] = 0;
            OP_Account['AccountEmpName'] = this.data.ChargeEmpName;
            //插入OP_Account
            strsql = "INSERT INTO [OP_Account]([LastDate], [TotalFee], [CashFee], [PromFee], [PosFee], [AccountEmpID], [AccountDate] \
            , [AccountFlag], [ReceivFlag], [ReceivEmpID], [ReceivDate], [ReceivBillNO], [WorkID], [InvoiceCount], [RefundInvoiceCount] \
            , [RoundingFee], [AccountType], [AccountEmpName]) \
            VALUES (@LastDate, @TotalFee, @CashFee,@PromFee, @PosFee, @AccountEmpID, @AccountDate, \
            @AccountFlag, @ReceivFlag, @ReceivEmpID, @ReceivDate, @ReceivBillNO, @WorkID, @InvoiceCount, @RefundInvoiceCount, \
            @RoundingFee, @AccountType, @AccountEmpName); \
            SELECT @@IDENTITY;";

            AccountID = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, OP_Account);
        }
        return AccountID;
    }
}